package com.yk.template.spring.normal.dao.common.impl;

import com.yk.template.spring.normal.dao.common.api.ICommonDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Project Name:spring-normal
 * File Name:CommonDaoImpl
 * Package Name:com.yk.template.spring.normal.dao.common.impl
 * Date:2017/9/18 14:05
 * Author:zhangju
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */

@Repository("commonDao")
public class CommonDaoImpl implements ICommonDao {

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    protected final Log log=LogFactory.getLog(CommonDaoImpl.class);

    /**
     * 获取session
     *
     * @return session
     */
    @Override
    public Session getSession() {
        return this.sessionFactory.openSession();
    }
    
    /**
     * 添加
     *
     * @param object 实体对象
     * @return 添加状态，true：成功，false：失败
     */
    @Override
    public boolean save(Object object) {
        boolean state=false;
        try{
            Session session=this.getSession();
            session.beginTransaction();
            session.save(object);
            session.getTransaction().commit();
            session.close();
            state=true;
        }catch (Exception e){
            this.log.error("hibernate entity save failed",e);
        }

        return state;
    }

    /**
     * 删除
     *
     * @param object 实体对象
     * @return 删除状态，true：成功，false：失败
     */
    @Override
    public boolean delete(Object object) {
        boolean state=false;
        try{
            Session session=this.getSession();
            session.beginTransaction();
            session.delete(object);
            session.getTransaction().commit();
            session.close();
            state=true;
        }catch (Exception e){
            this.log.error("hibernate entity delete failed",e);
        }

        return state;
    }

    /**
     * 更新
     *
     * @param object 实体对象
     * @return 更新状态，true：成功，false：失败
     */
    @Override
    public boolean update(Object object) {
        boolean state=false;
        try{
            Session session=this.getSession();
            session.beginTransaction();
            session.update(object);
            session.getTransaction().commit();
            session.close();
            state=true;
        }catch (Exception e){
            this.log.error("hibernate entity update failed",e);
        }

        return state;
    }

    /**
     * 获取与综合条件
     *
     * @param conditions 条件队列
     * @return 综合条件
     */
    @Override
    public String getAndCondition(HashSet<String> conditions) {
        StringBuilder oneCondition= new StringBuilder();
        if(conditions.isEmpty())
            return oneCondition.toString();

        Iterator<String> iterator=conditions.iterator();
        while (iterator.hasNext()){
            String condition=iterator.next();
            if(!condition.isEmpty()){
                oneCondition.append(condition);
                if(iterator.hasNext())
                    oneCondition.append(" and ");
            }
        }
        if(conditions.size()>1)
            oneCondition = new StringBuilder("(" + oneCondition + ")");
        return oneCondition.toString();
    }

    /**
     * 获取或综合条件
     *
     * @param conditions 条件队列
     * @return 综合条件
     */
    @Override
    public String getOrCondition(HashSet<String> conditions) {
        StringBuilder oneCondition= new StringBuilder();
        if(conditions.isEmpty())
            return oneCondition.toString();
        Iterator<String> iterator=conditions.iterator();
        while (iterator.hasNext()){
            String condition=iterator.next();
            if(!condition.isEmpty()){
                oneCondition.append(condition);
                if(iterator.hasNext())
                    oneCondition.append(" or ");
            }
        }
        if(conditions.size()>1)
            oneCondition = new StringBuilder("(" + oneCondition + ")");
        return oneCondition.toString();
    }
}
